﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BankEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid BankID = Guid.Empty;
    BanksManager manager = new BanksManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    var bank = manager.Single(s => s.RecordID == RecordID);

                    txtName.Text = bank.Name;
                    txtNameGeo.Text = bank.NameGeo;
                    txtBeginDate.DateValue = bank.BeginDate;
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            var CurrentUser = Session["User"] as UserHelper;

            Guid NewBankID = Guid.Empty;

            if (RecordID == Guid.Empty)
            {
                if (Guid.TryParse(Request.QueryString["sid"], out BankID))
                {
                    if (manager.Find(f => f.IsActive && f.BankID == BankID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("Bank already exists with this date");
                    }

                    manager.Add(new Bank
                    {
                        RecordID = Guid.NewGuid(),
                        BankID = BankID,
                        Name = txtName.Text,
                        NameGeo = txtNameGeo.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    });

                    NewBankID = BankID;
                }
                else
                {
                    var bank = new Bank
                    {
                        RecordID = Guid.NewGuid(),
                        BankID = Guid.NewGuid(),
                        Name = txtName.Text,
                        NameGeo = txtNameGeo.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    };

                    manager.Add(bank);

                    NewBankID = bank.BankID;
                }
            }
            else
            {
                var bank = manager.Single(s => s.RecordID == RecordID);

                if (manager.Find(f => f.IsActive && f.BankID == bank.BankID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                {
                    throw new Exception("Bank already exists with this date");
                }

                bank.Name = txtName.Text;
                bank.NameGeo = txtNameGeo.Text;
                bank.BeginDate = txtBeginDate.DateValue.Value;
                bank.LastModifyDate = DateTime.Now;
                bank.LastModifier = CurrentUser.UserID;

                NewBankID = bank.BankID;
            }

            manager.SaveChanges();
            manager.RepiarDates(NewBankID);

            if (BankID == Guid.Empty && RecordID == Guid.Empty)
            {
                Response.Redirect("bankview.aspx?m=liBanks");
            }
            else
            {
                Response.Redirect(String.Format("~/bankhistory.aspx?id={0}&m={1}", NewBankID, "liBanks"));
            }
        }
        catch(Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}